var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/functionalComponent/base-help/baseContent.tsx
var baseContent_exports = {};
__export(baseContent_exports, {
  BaseContent: () => BaseContent,
  Footer: () => Footer,
  Header: () => Header,
  HelpContext: () => HelpContext,
  MultipleButtons: () => MultipleButtons
});
module.exports = __toCommonJS(baseContent_exports);
var import_icons = require("@ant-design/icons");
var import_react = __toESM(require("react"));
var import_baseComponent = require("../../baseComponent");
var import_util = require("../../util");
var import_Button = require("../antd/Button");
var import_modal = require("../modal");
var import_table = require("../table");
var import_locale = require("./locale");
var HelpContext = (0, import_react.createContext)({
  observer: new import_util.Observer(),
  ok: () => {
  },
  close: () => {
  },
  request: (query) => Promise.resolve(query),
  contentParams: {},
  locale: import_locale.helpLocale,
  randomKey: ""
});
var buttonProps = {
  size: "small",
  style: { display: "block", width: 50, margin: "16px 10px" }
};
var tableProps = {
  rowSelected: false,
  rowChecked: true,
  cache: false,
  headerMenu: false,
  style: { border: "1px solid var(--border-color-split, #f0f0f0)" }
};
function Header(props) {
  var _a;
  const helpCtx = (0, import_react.useContext)(HelpContext);
  const [ins] = (0, import_modal.useModal)();
  return /* @__PURE__ */ import_react.default.createElement(
    import_modal.ModalHeader,
    {
      closable: true,
      close: helpCtx.close,
      fullscreenable: !!((_a = ins == null ? void 0 : ins.getApi()) == null ? void 0 : _a.setFullscreen),
      children: props.children,
      title: props.title || helpCtx.contentParams.helpTitle || helpCtx.locale.baseTitle
    }
  );
}
function Footer(props) {
  const helpCtx = (0, import_react.useContext)(HelpContext);
  const { okText = helpCtx.locale.OK, cancelText = helpCtx.locale.Cancel, getResult, children } = props;
  const okClick = async () => {
    var _a;
    const result = await getResult();
    return (_a = helpCtx.ok) == null ? void 0 : _a.call(helpCtx, result);
  };
  const cancelClick = () => {
    var _a;
    return (_a = helpCtx.close) == null ? void 0 : _a.call(helpCtx);
  };
  return /* @__PURE__ */ import_react.default.createElement("div", { style: { alignItems: "center", display: "flex", padding: 16 } }, /* @__PURE__ */ import_react.default.createElement("div", { style: { flex: 1, width: 0, display: "flex", alignItems: "center" } }, children), cancelText && /* @__PURE__ */ import_react.default.createElement(import_Button.Button, { onClick: cancelClick, style: { margin: "0 5px", minWidth: 68 } }, cancelText), okText && /* @__PURE__ */ import_react.default.createElement(import_Button.Button, { onClick: okClick, type: "primary", style: { margin: "0 5px", minWidth: 68 } }, okText));
}
function MultipleButtons(props) {
  const {
    getRowKey,
    addResult: defaultAddResult,
    removeResult: defaultRemoveResult,
    getActiveTable,
    getResultTable,
    outRef,
    style
  } = props;
  const addResult = defaultAddResult || ((values) => {
    const selected = [];
    const existValues = getResultTable().getRows().map((r) => getRowKey(r));
    values.forEach((value) => {
      if (existValues.indexOf(getRowKey(value)) === -1) {
        const newVal = JSON.parse(JSON.stringify(value));
        delete newVal.checked;
        selected.push(newVal);
      }
    });
    getResultTable().addRows(selected);
  });
  const removeResult = defaultRemoveResult || ((indexes) => {
    if (indexes === void 0) {
      getResultTable().setDataSource([]);
    } else {
      getResultTable().deleteRows(indexes);
    }
  });
  (0, import_util.useExtendRef)(outRef, {
    toLeft: removeResult,
    toRight: addResult
  });
  return /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      style: {
        width: 80,
        display: "flex",
        alignItems: "center",
        justifyContent: "center",
        ...style
      }
    },
    /* @__PURE__ */ import_react.default.createElement("div", null, /* @__PURE__ */ import_react.default.createElement(
      import_Button.Button,
      {
        ...buttonProps,
        icon: /* @__PURE__ */ import_react.default.createElement(import_icons.RightOutlined, null),
        onClick: () => addResult(getActiveTable().getSelectedData())
      }
    ), /* @__PURE__ */ import_react.default.createElement(import_Button.Button, { ...buttonProps, icon: /* @__PURE__ */ import_react.default.createElement(import_icons.DoubleRightOutlined, null), onClick: () => addResult(getActiveTable().getRows()) }), /* @__PURE__ */ import_react.default.createElement(
      import_Button.Button,
      {
        ...buttonProps,
        icon: /* @__PURE__ */ import_react.default.createElement(import_icons.LeftOutlined, null),
        onClick: () => removeResult(getResultTable().getSelectedIndexes())
      }
    ), /* @__PURE__ */ import_react.default.createElement(import_Button.Button, { ...buttonProps, icon: /* @__PURE__ */ import_react.default.createElement(import_icons.DoubleLeftOutlined, null), onClick: () => removeResult() }))
  );
}
function BaseContent() {
  const {
    request,
    ok,
    contentParams: {
      remote = true,
      autoLoad = true,
      FilterTree,
      columns,
      helpTitle,
      multiple,
      selectedArray = [],
      pagination = {
        align: "left"
      },
      getFieldValue
    },
    locale
  } = (0, import_react.useContext)(HelpContext);
  const id = (0, import_react.useMemo)(() => Math.random().toString(32).slice(2), []);
  const leftTable = (0, import_react.useRef)();
  const rightTable = (0, import_react.useRef)();
  const btnRef = (0, import_react.useRef)();
  const tableRef = (0, import_react.useMemo)(() => [leftTable, rightTable], []);
  const getRightTable = () => rightTable.current.getApi();
  const getLeftTable = () => leftTable.current.getApi();
  const getResult = () => {
    if (multiple) {
      const result = getRightTable().getRows().map((r) => ({
        value: getFieldValue(r, "value"),
        label: getFieldValue(r, "label"),
        origin: { ...r }
      }));
      return result.length > 0 ? result : void 0;
    } else {
      const value = getLeftTable().getSelectedData()[0];
      if (value) {
        return {
          value: getFieldValue(value, "value"),
          label: getFieldValue(value, "label"),
          origin: { ...value }
        };
      }
    }
  };
  const toRight = async (values) => {
    if (multiple) {
      btnRef.current.toRight(values);
    } else {
      const value = getResult();
      ok(value);
    }
  };
  const onSearch = (0, import_util.useRefCallback)((value) => {
    if (remote) {
      getLeftTable().query({ keyword: value });
    } else {
      getLeftTable().filter(value);
    }
  });
  const onSelectCallback = (0, import_util.useRefCallback)((keys, nodes) => {
    getLeftTable().setExtraParam({ treeNodes: nodes });
  });
  if (pagination) {
    pagination.targetContainer = id;
  }
  tableProps.checkbox = multiple ? "checked" : false;
  tableProps.rowSelected = !multiple;
  tableProps.rowChecked = multiple;
  return /* @__PURE__ */ import_react.default.createElement(import_util.Layout, null, /* @__PURE__ */ import_react.default.createElement(Header, { title: helpTitle || (multiple ? locale.multipleTitle : locale.singleTitle) }), /* @__PURE__ */ import_react.default.createElement(import_util.Layout.Flex, { direction: "row", style: { padding: "5px 5px 0 5px" } }, FilterTree && /* @__PURE__ */ import_react.default.createElement(
    import_util.Layout.Slider,
    {
      size: multiple ? 180 : 200,
      style: { marginRight: 5, border: "1px solid var(--border-color-split, #f0f0f0)" }
    },
    /* @__PURE__ */ import_react.default.createElement(FilterTree, { onSelectedChange: onSelectCallback, tableRef })
  ), /* @__PURE__ */ import_react.default.createElement(import_util.Layout.Flex, { direction: "column" }, /* @__PURE__ */ import_react.default.createElement(import_baseComponent.Search, { allowClear: true, placeholder: locale.searchPlaceholder, onSearch }), /* @__PURE__ */ import_react.default.createElement(import_util.Layout.Flex, null, /* @__PURE__ */ import_react.default.createElement(
    import_table.Table,
    {
      ...tableProps,
      style: { ...tableProps.style, borderTop: 0 },
      ref: leftTable,
      checkbox: !!multiple,
      columns: (columns == null ? void 0 : columns.left) || columns,
      pagination,
      onRow: (rowIndex, table) => ({
        onDoubleClick: () => toRight([table.getRow(rowIndex)])
      }),
      autoLoad,
      request
    }
  ))), multiple && /* @__PURE__ */ import_react.default.createElement(
    MultipleButtons,
    {
      outRef: btnRef,
      getRowKey: getFieldValue,
      getActiveTable: getLeftTable,
      getResultTable: getRightTable
    }
  ), multiple && /* @__PURE__ */ import_react.default.createElement(import_util.Layout.Flex, null, /* @__PURE__ */ import_react.default.createElement(
    import_table.Table,
    {
      ...tableProps,
      checkbox: true,
      dataSource: selectedArray,
      columns: columns.right || columns,
      ref: rightTable,
      onRow: (rowIndex) => ({
        onDoubleClick: () => btnRef.current.toLeft(rowIndex)
      })
    }
  ))), /* @__PURE__ */ import_react.default.createElement(Footer, { getResult }, /* @__PURE__ */ import_react.default.createElement("div", { id, style: { padding: "0 5px", width: "100%" } })));
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  BaseContent,
  Footer,
  Header,
  HelpContext,
  MultipleButtons
});
